package net.tycmc.cty.ctyms.timejob.common;

import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;

import org.apache.commons.configuration.ConfigurationException;

/**
 * 设备或终端与版本关系的通用方法类（目前只兼容412和414版本，其他版本不予支持）
 * @author zhangmeijuan
 *
 */
public class EquipVersionCommon {
	static IBaseDAO baseDAO;
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	/**
	 * @author zhangmeijuan 根据设备号，查询此设备安装的终端号的软件版本号
	 * @param VehicleNumber
	 * @return
	 */
	public static String getTerminalVersion(String VehicleNumber){
		if(StringUtil.isValid(VehicleNumber)){
			String sql="select Tmnl_SoftEdition from V_VehicleInfo where VehicleNumber='"+VehicleNumber+"' and GETDATE() between VehicleBuildTime and UsefulLife and GETDATE()<TmnlI_BackoutEnd";
			return checkSoftVersion(sql);
		}
		
		
		return null;
	}
	/**
	 * @author zhangmeijuan
	 * 根据设备id，查询此设备安装的终端号的软件版本号
	 * @param VehicleID
	 * @return
	 */
	public static String getTerminalVersionByID(String VehicleID){
		if(StringUtil.isValid(VehicleID)){
			String sql="select Tmnl_SoftEdition from V_VehicleInfo where VehicleID='"+VehicleID+"' and GETDATE() between VehicleBuildTime and UsefulLife and GETDATE()<TmnlI_BackoutEnd";
			return checkSoftVersion(sql);
		}
		return null;
	}
	
	/**
	 * @author zhangmeijuan  根据传入的sql语句根据设备号或是设备id查询软件版本号
	 * @param sql
	 * @return
	 */
	private static String checkSoftVersion(String sql){
		Map<String,Object> map=baseDAO.execQueryOne(sql, new Object[]{});
		if(map!=null && StringUtil.isValid(MapGetter.getString(map, "Tmnl_SoftEdition"))){
			return MapGetter.getString(map, "Tmnl_SoftEdition");
		}else{
			return null;
		}
	}
	
	/**
	 * 指定软件版本号是否支持指定参数
	 * @param paramType 参数类型，PP、J1939
	 * @param softVersion 软件版本号
	 * @return
	 */
	public static boolean isHoldParam(String paramType,String softVersion){
		if(!StringUtil.isNullOrEmpty(softVersion))return false;
		String holdSoftVersion = "";
		try {
			holdSoftVersion = DynamicReadConfig.getConfigProperty("softveragreement.properties", paramType);
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		if(holdSoftVersion.trim().toUpperCase().indexOf(softVersion.trim().toUpperCase())>=0)return true;
		return false;
	}
	/**
	 * 通过软件版本号获取协议号
	 * 输入一个软件版本号，只返回一个协议号（即使一个软件号对应多个协议号）
	 * 这样的目的：通过一个协议号进行程序判断即可。
	 * @param softVersion
	 * @return
	 */
	public static String getAgrNoBySoftVer(String softVer){
		if(StringUtil.isValid(softVer)){
			String agrNo = "";
			try {
				agrNo = DynamicReadConfig.getConfigProperty("softveragreement.properties", softVer.trim().toUpperCase());
			} catch (ConfigurationException e) {
				e.printStackTrace();
			}
			if(agrNo!=null&&agrNo.indexOf(",")>0)agrNo = agrNo.split(",")[0];
			return agrNo;
		}else{
			return null;
		}
	}
	/**
	 * 通过协议类型获取查询条件软件版本号
	 * @param agrType 如：414
	 * @return 如'cumm3.3','cumm3.3.1'
	 */
	public static String getSoftVerByAgrType(String agrType){
		StringBuffer termSoft = new StringBuffer();
		if(StringUtil.isNullOrEmpty(agrType)){
			try {
				String softVerS = DynamicReadConfig.getConfigProperty("softveragreement.properties", agrType.trim());
				//软件版本号拆分、拼接
				String [] softVerArr = softVerS.split(",");
				for(String softVer:softVerArr){
					if(StringUtil.isNullOrEmpty(softVer)){
						if(!termSoft.toString().equals(""))termSoft.append(",");
						termSoft.append("'"+softVer+"'");
					}
				}
			} catch (ConfigurationException e) {
				e.printStackTrace();
			}
		}
		return termSoft.toString();
	}
	
	/**
	 * 拼接软件版本号
	 * @param softVerArr 如：输入"'cumm3.3','cumm3.3.1'","'cumm3.4'"
	 * @return 输出"'cumm3.3','cumm3.3.1','cumm3.4'"
	 */
	public static String spltSoftVer(String ...softVerArr){
		StringBuffer softVers = new StringBuffer();
		for(String softVer:softVerArr){
			if(StringUtil.isValid(softVers))
				softVers.append(",");
			softVers.append(softVer);
		}
		return softVers.toString();
	}
}
